<?php

	class FTAWebPage extends FTAObject {
		
		var $webSnippet;
		var $zdb;

		var $documentType;
		
		function __construct(){
			parent::__construct();
			
			global $db;	
			$this->cmsDocument = new CmsDocument($db);
			global $FTAWebPageDocumentType;
			 
			$this->documentType = is_null($FTAWebPageDocumentType) ? 'docWebPage':$FTAWebPageDocumentType;
		}
		
		function openWebSnippet($pageIdentifier){
			
			//add additional cache checks here before we do all this shit..
			if (is_null($pageIdentifier))
				return false;
				
			$this->cmsDocument->flush();
			$this->cmsDocument->setActiveDocuments();
			$this->cmsDocument->addDocumentType($this->documentType);
			
			if (is_numeric( ($pageIdentifier * 1) ) &&  0 < ($pageIdentifier * 1)){
				$this->cmsDocument->addConstraint('documentcode',$pageIdentifier);	
			} else {
				$this->cmsDocument->addConstraint('<<md.urlfragment>>',$pageIdentifier);
			}
			
			$this->cmsDocument->setPageSize(1);
			
			$this->cmsDocument->resetIterator();
			
			if($this->cmsDocument->totalDocuments() == 1) {
				$this->webSnippet = $this->cmsDocument->nextDocumentFromCache();
               	return true;
			} else {
               	$this->webSnippet = array();		
				return false;
			}

		}
		
		
		//method for getting sub navigation items// we will get all websnippets that match
		// and all page builder pages.. fragments with a #xxxx will be excluded.. as will
		// /xxx/xxx/xxx/ dont want to stagger the nav too deep
		
		function &getSubNavigationForPage($pageIdentifier) {
		
				//if we have a number.. we have an extra step to take, as we need to get the
				// content first to look up the fragment..
				$subNavigation = array();
				
				$currentWebSnippet = $this->webSnippet;
								
				//$this->cmsDocument->flush();
				if ($this->openWebSnippet($pageIdentifier)) {
					$urlFragment = $this->webSnippet->urlfragment;
					
					$this->cmsDocument->flush();
					$this->cmsDocument->setActiveDocuments();
					$this->cmsDocument->addDocumentType($this->documentType);
					$this->cmsDocument->addDocumentType('pagebuilder');
					$this->cmsDocument->addConstraint('documentcode',$this->webSnippet->documentcode,'!=');
					$this->cmsDocument->addConstraint('<<md.urlfragment>>', $this->webSnippet->urlfragment .'%' ,'LIKE');
					
					//
					$this->cmsDocument->addSort('<<md.rank>>');
					
					$this->cmsDocument->setPageSize( null );
					
					$this->cmsDocument->resetIterator();
					$subNavigation = $this->cmsDocument->getAllDocuments();
					$subNavigation = array_map(
						array($this, 'map_webSnippetCmsDocumentRecordToLinkList'), $subNavigation);
					
				}

				$this->webSnippet = $currentWebSnippet;
				
				return $subNavigation;
		}
		
		function map_webSnippetCmsDocumentRecordToLinkList($o){
			$oo = new stdClass();
			$oo->destination = $o->urlfragment;
			$oo->text = $o->extra15;
	
			return $oo;			
		}
		
		function renderPageTitle(){
			if(is_null($this->webSnippet))
				return;			
?>
<title><?= $this->webSnippet->pagetitle ?></title>
<?			
		}
		
		function renderPageKeywords(){
			if(is_null($this->webSnippet))
				return;			
?>
<meta name="Keywords" content="<?= $this->webSnippet->keywords ?>" />
<?				
		}
		
		function renderPageDescription(){
			if(is_null($this->webSnippet))
				return;
?>
<meta name="Description" content="<?= $this->webSnippet->description ?>" />
<?			

		}
		
		function renderPageHeading(){
			
			if(is_null($this->webSnippet))
				return;			
			
			echo fta_widgetH1Tag($this->webSnippet->heading);
		}
		
		
		function renderPageHeadingAsSubHeading() {
			
			if(is_null($this->webSnippet))
				return;				
			
			echo fta_widgetH2Tag($this->webSnippet->heading);
		}
		

		
		function renderPageSubHeading(){

			if(is_null($this->webSnippet))
				return;				
			
			echo fta_widgetH2Tag($this->webSnippet->title);
		}
		
		
		function renderPageSubHeadingAsTagLine(){	

			if(is_null($this->webSnippet))
				return;				
			
			echo fta_widgetH5Tag($this->webSnippet->title);
			
		}
		
		function renderPageContent() {
			if(is_null($this->webSnippet))
				return;	

			echo $this->webSnippet->content;			
		}
				
		
		function renderPageSubNavigation(){
			if(is_null($this->webSnippet))
				return;				
			
			//$subNavigation = $this->getSubNavigationForPage();
			$subNavigation = $this->getSubNavigationForPage( $this->webSnippet->documentcode );
			echo fta_widgetNavigationList($subNavigation);			
		}
		
		
		// at this point. the render will not do too much .. but we will
		// work on it.
		function render(){}
		
		
	}
	
	
////////////


	//fta web page helpers
	
	
	/**
	 * this function adds the metadata to a page, it looks up conent
	 * via either a document code, or a url fragment and spits out the title, descrition and keywords 
	 * 
	 * @param String $pageIdentifier
	 * @return String 
	 *	  
	 */
	function fta_getPageMetaData($pageIdentifier) {
		$metaData="";
		$metaData .= fta_getPageTitle($pageIdentifier);
		$metaData .= fta_getPageKeywords($pageIdentifier);
		$metaData .= fta_getPageDescription($pageIdentifier);
		return $metaData;
	}
	
	/**
	 * Prints out a websnippet as a header title
	 *
	 * @param String $pageIdentifier
	 * @return String 
	 *
	 */
	function fta_getPageTitle($pageIdentifier){
		ob_start();
		fta_webPageOpenSnippetAndInvoke($pageIdentifier, 'renderPageTitle');										
		return ob_get_clean();		
	}
	
	/**
	 * returns the page keywords as a meta tag
	 *
	 * @param String $pageIdentifier
	 * @return String 
	 * 
	 */	
	function fta_getPageKeywords($pageIdentifier){
		ob_start();
		fta_webPageOpenSnippetAndInvoke($pageIdentifier, 'renderPageKeywords');								
		return ob_get_clean();
	}
	
	/**
	 * Retruns the page description as a meta tag
	 *
	 * @param String $pageIdentifier
	 * @return String 
	 * 
	 */	
	function fta_getPageDescription($pageIdentifier){
		ob_start();
		fta_webPageOpenSnippetAndInvoke($pageIdentifier, 'renderPageDescription');						
		return ob_get_clean();		
	}
	

	/**
	 * returns the page heading/h1 tag
	 *
	 * @param unknown_type $pageIdentifier
	 * @return String
	 * 
	 */
	function fta_getPageHeading($pageIdentifier){
		ob_start();
		fta_webPageOpenSnippetAndInvoke($pageIdentifier, 'renderPageHeading');				
		return ob_get_clean();		
	}

	/**
	 * returns the page content
	 *
	 * @param unknown_type $pageIdentifier
	 * @return String
	 * 
	 */
	function fta_getPageContent($pageIdentifier){
		ob_start();
		fta_webPageOpenSnippetAndInvoke($pageIdentifier, 'renderPageContent');		
		return ob_get_clean();		
	}

	/**
	 * returns the page heading/h1 tag as an H2 tag
	 *
	 * @param unknown_type $pageIdentifier
	 * @return String
	 * 
	 */	
	function fta_getPageHeadingAsSubHeading($pageIdentifier){
		ob_start();
		fta_webPageOpenSnippetAndInvoke($pageIdentifier, 'renderPageHeadingAsSubHeading');
		return ob_get_clean();		
	}
	
	
	function fta_getPageSubHeading($pageIdentifier){
		ob_start();
		fta_webPageOpenSnippetAndInvoke($pageIdentifier, 'renderPageSubHeading');
		return ob_get_clean();	
	}
	
	function fta_getPageSubHeadingAsTagLine($pageIdentifier){
		ob_start();
		fta_webPageOpenSnippetAndInvoke($pageIdentifier, 'renderPageSubHeadingAsTagLine');
		return ob_get_clean();			
	}
	
////
	
	function fta_getPageSubNavigation($pageIdentifier){
		ob_start();
		fta_webPageOpenSnippetAndInvoke($pageIdentifier, 'renderPageSubNavigation');
		return ob_get_clean();			
	}
	
	
	/**
	 * This method returns a page Identifier from the request, and attempts to pre load the page.
	 * If it cant load the page it will throw an error. otherwise we get the pageIdent back
	 *
	 */
	function fta_getPageIdentifierFromRequestOr404() {
		//have modified this so that 
		$pageIdentifier = extractStringWithKeyFromArray('REQUEST_URI',$_SERVER,null);
		$pageIdentifier = extractStringWithKeyFromArray('REDIRECT_URL',$_SERVER,$pageIdentifier);
		
		$webSnippetObject = &fta_getSharedWebSnippetObject();

		$ws  = $webSnippetObject->openWebSnippet( $pageIdentifier );
		if (!is_null($pageIdentifier) && $ws){
			return $pageIdentifier;
		}
		
		//nothing. do a fail..
		fta_redirectCleanly('/html/404.php');	
	}
	
	
///////
	
	
	/**
	 * This method, gets the fta object, opens the page with the given identifier and then invokes the mehtod.
	 * Typically used to call the render methods of a page/snippet
	 *
	 * @param unknown_type $method
	 * @param unknown_type $objectGetter
	 */
	function fta_webPageOpenSnippetAndInvoke($pageIdentifier,$method){
		$webSnippetObject = &fta_getSharedWebSnippetObject();
		if ( $webSnippetObject->openWebSnippet( $pageIdentifier ) ) {
			$webSnippetObject->$method();
		}
	}
	
	function fta_mapUrl($url){
		$url = str_replace('&','-AND-',$url);
		$url = str_replace('/','---',$url);
		return urlencode($url);
	}
	
	function fta_reverseMapUrl($url) {
		$url = urldecode($url);
		$url = str_replace('---','/',$url);		
		return str_replace('-AND-','&',$url);
	}
	
	/**
	 * Gets the fta obejct.. these are singletons really..
	 *
	 * @return FTAWebPage
	 */
	function fta_getSharedWebSnippetObject(){

		global $fta_;

		global $FTAWebPageClass;
		
		if(!is_null($FTAWebPageClass) && "FTAWebPage" != $FTAWebPageClass){
		// This is a bit icky, as we need the page and the um.. thing.. we will assum that 
		// the class is included at this point in time.. 
		//	fta_moduleListMapAndInclude($FTAWebPageClass);
			$c = $FTAWebPageClass;
		} else {
			$c = "FTAWebPage";
		}
			
		if (!array_key_exists('ftaWebPage',$fta_)){
			$fta_->ftaWebPage = new  $c();
		}
		return $fta_->ftaWebPage;
	}
	